<?php

class FUserGoldCoinFrozen extends BaseModel
{
    public $res;
    public $expire;

    public function getSource()
    {
        return "f_user_gold_coin_frozen";
    }

    public function get_list($params, $method = 'list')
    {

        $page = isset($params['page']) ? intval($params['page']) : 1;
        $size = isset($params['page']) ? intval($params['limit']) : 10;
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $conditions = "1 = 1";
        if (isset($params['start_time']) && !empty($params['start_time'])) {
            $conditions .= " and created_at >= '{$params['start_time']}'";
        }

        if (isset($params['end_time']) && !empty($params['end_time'])) {
            $conditions .= " and created_at <= '{$params['end_time']}'";
        }
        if (isset($params['status'])) {
            $conditions .= " and status = '{$params['status']}'";
        }

        if (isset($params['phone']) && !empty($params['phone'])) {
            $userDb = new User();
            $info   = $userDb->findFirstByPhone($params['phone']);
            if (!$info) {
                return [
                    'count' => 0,
                    'total' => 0,
                    'list' => [],
                ];
            }
            $conditions .= " and user_id = $info->id";
        }
        $cate_list['count'] = $this->countGold($conditions);
        $cate_list['list']  = $this::find(
            array(
                $conditions,
                'columns' => 'user_id,num,remarks,created_at',
                "order"   => 'created_at desc',
                'limit'   => $size,
                'offset'  => $page
            ))->toarray();
        $cate_list['total'] = $this::count(
            array(
                $conditions,
            ));
        return $cate_list;
    }

    public function countGold($condition)
    {
        $where['conditions'] = $condition;
        $where['columns']    = "sum(num) as num";
        $num                 = $this->findFirst($where);
        return $num->num ? $num->num : 0;
    }

    public function get_count($params = [])
    {
        $conditions = "type = 2 and status = 0";
        if (isset($params['start_at'])) {
            $conditions .= " and order_earning_time >= '{$params['start_at']}' and order_earning_time <= '{$params['end_at']}'";
        }
        $where['conditions'] = $conditions;
        $where['columns']    = 'sum(num) as count';
        $count               = $this->findFirst($where);
        return $count->count ? $count->count : 0;
    }


    public function get_count_new($params = [])
    {
        $conditions = "status = 0";
        if (isset($params['start_at'])) {
            $conditions .= " and order_earning_time <= '{$params['start_at']}'";
        }
        $where['conditions'] = $conditions;
        $where['columns']    = 'sum(num) as count';
        $count               = $this->findFirst($where);
        return $count->count ? $count->count : 0;
    }

    public function get_count_new_2($params = [])
    {
        $conditions = "status = 0";
        if (isset($params['start_time'])) {
            $conditions .= " and created_at >= '{$params['start_time']}' AND created_at<= '{$params['end_time']}'";
        }
        $where['conditions'] = $conditions;
        $where['columns']    = 'sum(num) as count';
        $count               = $this->findFirst($where);
        return $count->count ? $count->count : 0;
    }


    public function numAll()
    {
        $where['conditions'] = "status = 0";
        $where['columns']    = "sum(num) as num";
        $num                 = $this->findFirst($where);
        return $num->num ? $num->num : 0;
    }
}
